package com.rain.shoppingscienceproj.utils;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.rain.shoppingscienceproj.Config;
import com.rain.shoppingscienceproj.http.BaseApiService;
import com.rain.shoppingscienceproj.service.ImageService;
import com.rain.shoppingscienceproj.service.OssService;
import com.rain.shoppingscienceproj.ui.view.UIDisplayer;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

public class OssUtils {
    //负责所有的界面更新
    private static UIDisplayer mUIDisplayer;
    //OSS的上传下载
    private static OssService mService;
    private static ImageService mIMGService;

    private static String mImgEndpoint = "http://img-cn-hangzhou.aliyuncs.com";
    private static final String mBucket = Config.BUCKET_NAME;

    private static Activity activity;


    public static void oss(Activity activity, ImageView head_photo, ProgressBar bar, TextView textView, int type) {
        OssUtils.activity = activity;
        // 第二个参数负责输出结果
        mUIDisplayer = new UIDisplayer(head_photo, bar, textView, activity, type);
        mService = initOSS(Config.OSS_ENDPOINT, Config.BUCKET_NAME, mUIDisplayer);

        //设置上传的callback地址，目前暂时只支持putObject的回调
//        mService.setCallbackAddress(Config.OSS_CALLBACK_URL);

        //图片服务和OSS使用不同的endpoint，但是可以共用SDK，因此只需要初始化不同endpoint的OssService即可
        mIMGService = new ImageService(initOSS(mImgEndpoint, mBucket, mUIDisplayer));
        /*  设置相关配置  */
        mService.setBucketName(Config.BUCKET_NAME);
        String newOssEndpoint = "http://oss-cn-beijing.aliyuncs.com";
        String newImageEndpoint = "http://img-cn-beijing.aliyuncs.com";
        OSSCredentialProvider credentialProvider;
        credentialProvider = new OSSAuthCredentialsProvider(Config.STS_SERVER_URL);
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSS oss = new OSSClient(activity.getApplicationContext(), newOssEndpoint, credentialProvider, conf);
        mIMGService = new ImageService(initOSS(newImageEndpoint, Config.BUCKET_NAME, mUIDisplayer));
        mService.initOss(oss);

        mUIDisplayer.settingOK();
        Intent data = new Intent();
        data.putExtra("bucketName", Config.BUCKET_NAME);
        data.putExtra("url", Config.STS_SERVER_URL);
        data.putExtra("endpoint", newOssEndpoint);
        activity.setResult(activity.RESULT_OK, data);
    }

    public static OssService initOSS(String endpoint, String bucket, UIDisplayer displayer) {
        OSSCredentialProvider credentialProvider;
        //使用自己的获取STSToken的类
        String stsServer = BaseApiService.Base_URL + "/aliController/assumeRole/";
        if (TextUtils.isEmpty(stsServer)) {
            credentialProvider = new OSSAuthCredentialsProvider(stsServer);
//            ToastUtils.showShortToast(STS_SERVER_URL);
        } else {
            credentialProvider = new OSSAuthCredentialsProvider(stsServer);
        }

        String editBucketName = Config.BUCKET_NAME;
        if (TextUtils.isEmpty(editBucketName)) {
            editBucketName = bucket;
            ToastUtils.showShortToast(bucket);
        }
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSS oss = new OSSClient(activity.getApplicationContext(), endpoint, credentialProvider, conf);
        OSSLog.enableLog();
        return new OssService(oss, editBucketName, displayer);
    }

    public static void setmService(String pathName, String path) {
        mService.asyncPutImage(pathName, path);
    }
}
